package hu.bme.aut.consulttime.client.components;

import java.util.List;

import hu.bme.aut.consulttime.client.TeamService;
import hu.bme.aut.consulttime.client.TeamServiceAsync;
import hu.bme.aut.consulttime.entitydtos.TeamDTO;
import hu.bme.aut.consulttime.entitydtos.UserDTO;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

public class TeamInfoPage extends Composite {
	
	private long teamId;
	
	private VerticalPanel mainPanel;
	private Label lblTeamName;
	private Label lblSemester;
	private FlexTable teamMembersFlexTable;
	
	private TeamServiceAsync teamService = GWT.create(TeamService.class);
	
	public TeamInfoPage() {
		super();
		
		mainPanel = new VerticalPanel();
		initWidget(mainPanel);
		mainPanel.setSize("100%", "100%");
		
		VerticalPanel verticalPanel_2 = new VerticalPanel();
		mainPanel.add(verticalPanel_2);
		verticalPanel_2.setSize("100%", "");
		verticalPanel_2.setSpacing(5);
		
		HorizontalPanel horizontalPanel_2 = new HorizontalPanel();
		verticalPanel_2.add(horizontalPanel_2);
		horizontalPanel_2.setWidth("100%");
		
		Label lblNewLabel_3 = new Label("Semester:");
		horizontalPanel_2.add(lblNewLabel_3);
		lblNewLabel_3.setWidth("80px");
		
		lblSemester = new Label("");
		horizontalPanel_2.add(lblSemester);
		lblSemester.setWidth("100%");
		horizontalPanel_2.setCellWidth(lblSemester, "100%");
		
		HorizontalPanel horizontalPanel_1 = new HorizontalPanel();
		verticalPanel_2.add(horizontalPanel_1);
		horizontalPanel_1.setWidth("100%");
		
		Label lblNewLabel_1 = new Label("Team name:");
		horizontalPanel_1.add(lblNewLabel_1);
		lblNewLabel_1.setWidth("80px");
		
		lblTeamName = new Label("");
		horizontalPanel_1.add(lblTeamName);
		lblTeamName.setWidth("100%");
		horizontalPanel_1.setCellWidth(lblTeamName, "100%");
		
		HorizontalPanel horizontalPanel_3 = new HorizontalPanel();
		verticalPanel_2.add(horizontalPanel_3);
		horizontalPanel_3.setWidth("100%");
		
		Label lblTeamMembers = new Label("Team members:");
		horizontalPanel_3.add(lblTeamMembers);
		
		teamMembersFlexTable = new FlexTable();
		mainPanel.add(teamMembersFlexTable);
		mainPanel.setCellHorizontalAlignment(teamMembersFlexTable, HasHorizontalAlignment.ALIGN_RIGHT);
		teamMembersFlexTable.setBorderWidth(1);
		teamMembersFlexTable.setSize("100%", "100%");
		teamMembersFlexTable.setText(0, 0, "Full name");
		teamMembersFlexTable.setText(0, 1, "Username");
		teamMembersFlexTable.setText(0, 2, "E-mail");
	}
	
	public long getTeamId() {
		return teamId;
	}

	public void setTeamId(long teamId) {
		this.teamId = teamId;
	}

	public void loadTeam() {
		teamService.retrieveTeam(teamId, new AsyncCallback<TeamDTO>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Remote Procedure Call - Failure");
			}

			@Override
			public void onSuccess(TeamDTO result) {
				lblSemester.setText(result.getSemesterId());
				lblTeamName.setText(result.getName());
				
			}
			
		});
		
	}
	
	public void loadTeamMembers() {
		teamMembersFlexTable.removeAllRows();
		teamMembersFlexTable.setText(0, 0, "Full name");
		teamMembersFlexTable.setText(0, 1, "Username");
		teamMembersFlexTable.setText(0, 2, "E-mail");
		
		teamService.retrieveMembers(teamId, new AsyncCallback<List<UserDTO>>() {
			
			@Override
			public void onSuccess(List<UserDTO> result) {
				int rownum = 1;
				for (UserDTO userDTO : result) {
					teamMembersFlexTable.setText(rownum, 0, userDTO.getFirstName() + " " + userDTO.getLastName());
					teamMembersFlexTable.setText(rownum, 1, userDTO.getUserName());
					teamMembersFlexTable.setText(rownum, 2, userDTO.getEmail());
					rownum++;
				}
				
			}
			
			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Remote Procedure Call - Failure");				
			}
		});
		
	}
}
